//
//  LykDaoHangView.m
//  GiftSay
//
//  Created by dllo on 16/1/7.
//  Copyright © 2016年 liyinkai. All rights reserved.
//

#import "LykDaoHangView.h"
#import "HomeDaoHangCollectionViewCell.h"
@interface LykDaoHangView ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, strong) UIView *daoHangView;
@property (nonatomic, strong) UICollectionView *collectionview;
@property (nonatomic, assign) NSInteger indexpathItem;
@end

@implementation LykDaoHangView

-(instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}
-(void)setDaoHangArray:(NSArray *)daoHangArray {
    _daoHangArray = daoHangArray;
    self.daoHangView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width - self.height, self.height)];
    [self addSubview:self.daoHangView];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.itemSize = CGSizeMake(self.daoHangView.width / 5, self.daoHangView.height);
    
    self.collectionview = [[UICollectionView alloc] initWithFrame:self.daoHangView.bounds collectionViewLayout:layout];
    self.collectionview.delegate = self;
    self.collectionview.dataSource = self;
    self.collectionview.pagingEnabled = NO;
    self.collectionview.bounces = NO;
    self.collectionview.backgroundColor = [UIColor whiteColor];
    self.collectionview.showsVerticalScrollIndicator = FALSE;
    self.collectionview.showsHorizontalScrollIndicator = FALSE;
    [self.collectionview registerClass:[HomeDaoHangCollectionViewCell class] forCellWithReuseIdentifier:@"HomeDaoHangCollectionViewCell"];
    [self.daoHangView addSubview:self.collectionview];
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HomeDaoHangCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeDaoHangCollectionViewCell" forIndexPath:indexPath];
    cell.model = self.daoHangArray[indexPath.item];
    return cell;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.daoHangArray.count - 1;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    self.scrollBlock(indexPath.item);
    HomeDaoHangCollectionViewCell *cell = (HomeDaoHangCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    cell.bottom.backgroundColor = [UIColor redColor];
    self.indexpathItem = indexPath.item;
}

-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
    HomeDaoHangCollectionViewCell *cell = (HomeDaoHangCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    cell.bottom.backgroundColor = [UIColor whiteColor];
}

-(void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.item != self.indexpathItem) {
        HomeDaoHangCollectionViewCell *cell1 = (HomeDaoHangCollectionViewCell *)cell;
        cell1.bottom.backgroundColor = [UIColor whiteColor];
    } else {
        HomeDaoHangCollectionViewCell *cell1 = (HomeDaoHangCollectionViewCell *)cell;
        cell1.bottom.backgroundColor = [UIColor redColor];
    }
    
}

-(void)getblock:(block)scrollBlock {
    self.scrollBlock = scrollBlock;
}

-(void)setX:(CGFloat)x {
    _x = x;
    CGFloat xx = x * self.collectionview.contentSize.width;
    [self.collectionview setContentOffset:CGPointMake(xx, 0) animated:YES];
    NSLog(@"%f", self.collectionview.contentOffset.x);
    if ((NSInteger)self.collectionview.contentOffset.x % (NSInteger)(self.width - self.height) == 0) {
        NSIndexPath *path = [NSIndexPath indexPathForItem:self.collectionview.contentOffset.x / (self.width - self.height) inSection:0];
        HomeDaoHangCollectionViewCell *cell = (HomeDaoHangCollectionViewCell *)[self.collectionview cellForItemAtIndexPath:path];
        cell.bottom.backgroundColor = [UIColor redColor];
    }
}

@end
